from fastapi import APIRouter, Depends

from apps.public import cruds, schemas
from apps.public.params.catalog import CatalogParams
from core.auth import Auth, FullAdminAuth
from core.dependencies import IdList
from core.response import SuccessResponse

router = APIRouter(prefix="/catalog")


@router.post("/create", summary="创建目录")
async def create_catalog(
        data: schemas.Catalog,
        auth: Auth = Depends(FullAdminAuth(permissions=["public.catalog.create"]))
):
    return SuccessResponse(
        await cruds.CatalogDal(auth.db).create_data(data=data),
        msg="创建成功"
    )


@router.get("/tree", summary="获取目录树")
async def list_catalog_tree(
        params: CatalogParams = Depends(),
        auth: Auth = Depends(FullAdminAuth())
):
    return SuccessResponse(await cruds.CatalogDal(auth.db).get_catalog_tree(**params.dict()))


@router.put("/update/{data_id}", summary="更新目录")
async def update_catalog(
        data_id: int,
        data: schemas.Catalog,
        auth: Auth = Depends(FullAdminAuth(permissions=["public.catalog.update"]))
):
    return SuccessResponse(
        await cruds.CatalogDal(auth.db).put_data(data_id, data),
        msg="修改成功"
    )


@router.delete("/delete", summary="删除目录")
async def delete_catalog(
        ids: IdList = Depends(),
        auth: Auth = Depends(FullAdminAuth(permissions=["public.catalog.delete"]))
):
    await cruds.CatalogDal(auth.db).delete_datas(ids.ids, v_soft=True)
    return SuccessResponse(msg="删除成功")
